<?php

namespace App\Services;

use Illuminate\Support\Facades\Redis;

class LiveC
{
    //生成推流地址
    function push_url($push_domain, $push_key, $expireTime, $appName, $streamName)
    {
        $push_url = '';
        //未开启鉴权Key的情况下
        if ($push_key == '') {
            $push_url = 'rtmp://' . $push_domain . '/' . $appName . '/' . $streamName;
            return $push_url;
        }
        $timeStamp = time() + $expireTime;
        $sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $push_key;
        $md5hash = md5($sstring);
        $push_url = 'rtmp://' . $push_domain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $md5hash;
        return $push_url;
    }

    //生成播流地址
    function play_url($play_domain, $play_key, $expireTime, $appName, $streamName)
    {
        //未开启鉴权Key的情况下
        if ($play_key == '') {
            $rtmp_play_url = 'rtmp://' . $play_domain . '/' . $appName . '/' . $streamName;
            $flv_play_url = 'http://' . $play_domain . '/' . $appName . '/' . $streamName . '.flv';
            $hls_play_url = 'http://' . $play_domain . '/' . $appName . '/' . $streamName . '.m3u8';
        } else {
            $timeStamp = time() + $expireTime;
            $rtmp_sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $play_key;
            $rtmp_md5hash = md5($rtmp_sstring);
            $rtmp_play_url = 'rtmp://' . $play_domain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $rtmp_md5hash;

            $flv_sstring = '/' . $appName . '/' . $streamName . '.flv-' . $timeStamp . '-0-0-' . $play_key;
            $flv_md5hash = md5($flv_sstring);
            $flv_play_url = 'http://' . $play_domain . '/' . $appName . '/' . $streamName . '.flv?auth_key=' . $timeStamp . '-0-0-' . $flv_md5hash;

            $hls_sstring = '/' . $appName . '/' . $streamName . '.m3u8-' . $timeStamp . '-0-0-' . $play_key;
            $hls_md5hash = md5($hls_sstring);
            $hls_play_url = 'http://' . $play_domain . '/' . $appName . '/' . $streamName . '.m3u8?auth_key=' . $timeStamp . '-0-0-' . $hls_md5hash;
        }
        $data = [
            'rtmp' => $rtmp_play_url,
            'flv' => $flv_play_url,
            'hls' => $hls_play_url
        ];
        return $data;
    }

    public function getURl()
    {
        //推流域名
        $push_domain = 'satui.zxh666.top';
        //推流域名配置的鉴权Key
        $push_key = 'dojugG0c1mpKGoPk';
        //生成随机的AppName
        $appName = md5(uniqid(microtime(true), true));
        //生成随机的StreamNma
        $streamName = md5(uniqid(microtime(true), true));
        $expireTime = 3600*3;
        //播放域名
        $play_domain = 'sabo.zxh666.top';
        //播放域名配置的鉴权Key
        $play_key = '5tUOofboDin7Ordv';
//        Redis::set('liveUrl',$appName.'asd'.$streamName);
        $push_url = $this->push_url($push_domain, $push_key, $expireTime, $appName, $streamName);
        $play_url = $this->play_url($play_domain, $play_key, $expireTime, $appName, $streamName);
        return [
            'push_url' => $push_url,
            'play_url' => $play_url,
            'appName'  => $appName,
            'streamName'=>$streamName
        ];
    }


}